home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Count 1.0.1 / count.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  3.7 KB  |  183 lines  |  [TEXT/CWIE]

  1. // Count
  2. // version 1.0.1
  3. // ported to THINK by Ken Long <kenlong@netcom.com>
  4. // updated for THINK 7 and CW7 on 951201
  5.  
  6. /* count -  By:  Jeff Beadles    jeff@quark.WV.TEK.COM
  7.  
  8.  
  9.     This program will count from the starting number to the stop
  10.     number, using the character 'fs' as the field seperator.
  11.  
  12.     Note, that fs may be in several forms:
  13.     -A    will use the letter 'A'
  14.     --    will use a '-' as fs, and
  15.     -\011    will use a tab (Octal 011) as the fs.  (sh does the expansion.)
  16.  
  17.     Bugs may be sent to me if desired.
  18.     Please keep your flames to yourself.  What do you expect for free?
  19. */
  20.  
  21. /*
  22. # Makefile for count.  This is a little overkill, but what the heck.
  23. # (This is public domain too!)
  24. # Written by:  Jeff Beadles
  25. # jeff@quark.WV.TEK.COM        ...tektronix!quark.wv!jeff
  26. #
  27.  
  28. CC = cc
  29. CFLAGS =
  30.  
  31. #For the executable file
  32. BINDIR=/usr/bin
  33.  
  34. count: count.c Makefile
  35.     $(CC) $(CFLAGS) count.c -o count
  36.  
  37. install: count
  38.     -strip count
  39.     cp count ${BINDIR}/count
  40.     chmod 755 ${BINDIR}/count
  41.  
  42. clean:
  43.     rm -f *.o core a.out
  44.  
  45. clobber: clean
  46.     rm -f count
  47.  
  48. */
  49.  
  50. /* 
  51. .\"
  52. .\" @(#)count        1.0    05/09/89
  53. .\"
  54. .TH COUNT 1 "09 MAY 1989"
  55. .UC 4
  56. .SH NAME
  57. count \- count numbers from a start to a stop point.
  58. .SH SYNOPSIS
  59. .B count [-c] start stop
  60. .SH DESCRIPTION
  61. .I Count
  62. will count thru an integer sequence of numbers from
  63. .I Start
  64. to
  65. .I Stop
  66. with a newline after each number.
  67.  
  68. Optionally,
  69. .I -c
  70. may be on the command line.  This may be in one of two forms.
  71. .I -$
  72. will put a 
  73. .I $
  74. between each number.
  75. .I -040
  76. will put a space (Octal
  77. .I 040
  78. ) between each number.
  79.  
  80. .SH AUTHOR
  81. Jeff Beadles    jeff@quark.WV.TEK.COM
  82. */
  83.  
  84. /*    Count.c  Released into the public domain on 05/09/89
  85.  *    Written by:  Jeff Beadles  jeff@quark.WV.TEK.COM
  86.  *      or ...!tektronix!quark.WV!jeff
  87.  *
  88.  *    NOTE:  This program is not supported by Tektronix, Inc.
  89.  *
  90.  *    This program will count from the starting number to the stop
  91.  *    number, using the character 'fs' as the field seperator.
  92.  *    Note, that fs may be in several forms:
  93.  *    -A    will use the letter 'A'
  94.  *    --    will use a '-' as fs, and
  95.  *    -\011    will use a tab (Octal 011) as the fs.  (sh does the expansion.)
  96.  *
  97.  *    Bugs may be sent to me if desired.
  98.  *    Please keep your flames to yourself.  What do you expect for free?
  99.  *
  100.  */
  101.  
  102.  
  103. #include <stdio.h>
  104. #include <ctype.h>
  105. #include <console.h>
  106.  
  107. /*
  108.  *    Default field separator
  109.  */
  110.  
  111. #ifndef FS
  112. #define FS '\n'
  113. #endif
  114.  
  115. int main (int argc, char **argv)
  116. {
  117.     void usage();
  118.     int oatc();
  119.     int    start = 0;    /* Start count                */
  120.     int    stop  = 0;    /* Stop  count                */
  121.     int    pos   = 1;    /* Position in command line for parsing    */
  122.     char    fs    = FS;    /* Field Separator            */
  123.  
  124.     argc = ccommand (&argv);
  125.     
  126.     if ( argc < 2)
  127.         usage(argv[0]);        /* Does not return */
  128.  
  129.     if ( argv[1][0] == '-' ) {
  130.         if ( (isdigit(argv[1][1])) && (strlen(argv[1]) == 4) )
  131.             fs=oatc(argv[1] + 1);
  132.             else
  133.             fs = argv[1][1];
  134.         pos++;        /* On to the next arg... */
  135.     }
  136.     start = atoi(argv[pos++]);    /* Start here, and... */
  137.  
  138.     if ( argc <= pos)
  139.         usage(argv[0]);        /* Does not return */
  140.  
  141.     stop  = atoi(argv[pos]);     /* Stop here. */
  142.     if ( start >= stop)        /* Are they brain damaged? */
  143.     {
  144.         fprintf(stderr,"Error:  START must be less than STOP\n");
  145.         exit(-2);
  146.     }
  147.  
  148. /*
  149.    Yes, this is it.  It even prints a '\n' when done, if the fs != '\n' (Wow)
  150.  */
  151.     while ( start <= stop )
  152.         printf("%d%c",start++,( (start != stop) ? fs : '\n' ) );
  153. }
  154.  
  155. /*
  156.    Can you figure out this function with no comments?  Sure, you can.
  157. */
  158. void usage (char *program)
  159. {
  160.     fprintf(stderr,"Usage: %s [ -c] start stop\n",program);
  161.     exit(-1);
  162. }
  163.  
  164. /*
  165.  *    octal ascii to char
  166.  */
  167.  
  168. int oatc (char *str)
  169. {
  170.     int retval=0;
  171.     int pos=0;
  172.     int tmp=0;
  173.     int loop;
  174.     static int table[] = { 1, 8, 64 };   /* Powers of 8, to avoid POW */
  175.  
  176.  
  177.     for(loop=strlen(str) - 1; loop >= 0; loop--)
  178.         retval += ( (str[loop] - '0') * table[pos++] );
  179.  
  180.     return((char)retval);
  181. }
  182.  
  183.